<?php

namespace App\Http\Controllers\Admin;
use App\Models\Admin\Permission;
use App\Http\Requests;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Requests\Admin\Permission\CreateRequest;
use App\Http\Requests\Admin\Permission\UpdateRequest;
use Style;

class PermissionController extends CommonController{

    /**
     * 构造函数
     * PermissionController constructor.
     */
    public function __construct(){
//        $this->middleware('checkPermission:Permission|Permission-index', ['only' => ['index']]);
//        $this->middleware('checkPermission:Permission|Permission-create', ['only' => ['create', 'store']]);
//        $this->middleware('checkPermission:Permission|Permission-edit', ['only' => ['edit', 'update']]);
//        $this->middleware('checkPermission:Permission|Permission-destroy', ['only' => ['destroy']]);

//        $this->middleware('checkPermission:Permission|Permission-index', ['only' => ['index']]);
      //  $this->middleware('permission:Permission-create', ['only' => ['create', 'store']]);
    }

    /**
     * 权限列表
     * @return mixed
     */
    public function index(){
        $permissionPage = Permission::paginate(10);
        $permission = Permission::orderBy('sort','asc')->paginate(10)->toArray();
        //设置无限级等级处理
        $permissions = (new Permission())->cateLevel($permission['data']);
        return Style::view('admin.rbac.permissions.index',compact('permissionPage','permissions'));
    }

    /**
     * 添加权限
     * @param string $id
     * @return mixed
     */
    public function create($id = ''){
        $permissionId = $id;
        $permission = Permission::where('fid', '=', '0')->get();
        return Style::view('admin.rbac.permissions.create',compact('permission','permissionId'));
    }

    /**
     * 添加权限提交
     * @param CreateRequest $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(CreateRequest $request){
        $result = Permission::create($request->all());
        if($result){
            flash('权限添加成功', 'success');
            return redirect('admin/permission');
        }else{
            return back()->with('errors','权限添加失败，请稍后重试！');
        }
    }

    /**
     * 权限列表排序
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function changeOrder(Request $request){
        $input = $request->all();
        $permission = Permission::find($input['id']);
        $permission->sort = $input['sort'];
        $result = $permission->update();
        if($result){
            return response()->json(['status' => 0,'msg' => '权限排序更新成功！']);
        }else{
            return response()->json(['status' => 1,'msg' => '权限排序更新失败，请稍后重试！']);
        }
    }

    /**
     * 编辑权限
     * @param $id
     * @return mixed
     */
    public function edit($id){
        $permissionArr = Permission::where('fid', '=', '0')->get()->toArray();
        $permission = (new Permission())->getPermission($permissionArr,$id);  //获取当前权限
        $currPermission = Permission::find($id);
        return Style::view('admin.rbac.permissions.edit',compact('permission','currPermission'));
    }

    /**
     * 更新权限
     * @param UpdateRequest $request
     * @param $id
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function update(UpdateRequest $request,$id){
        $input = $request->except(['_method','_token']);
        $result = Permission::where('id',$id)->update($input);
        if($result){
            flash('更新权限成功', 'success');
            return redirect('admin/permission');
        }else{
            return back()->with('errors','权限添加失败，请稍后重试！');
        }
    }

    /**
     * 删除权限
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function destroy($id){
        $permissionCon = Permission::all();
        $getId = (new Permission())->getChildId($permissionCon,$id);  //获取当前父级id下的所有无限级子分类ID
        array_push($getId,$id);
        $result = Permission::whereIn('id',$getId)->delete();
        if($result){
            DB::table('permission_role')->whereIn('permission_id',$getId)->delete();
            return response()->json(['status' => 0,'msg' => '权限删除成功！']);
        }else{
            return response()->json(['status' => 1,'msg' => '权限删除失败，请稍后重试！']);
        }
    }

}